/*  mudmap - A tool to create and organize user side maps for MUD games
 *  Copyright (C) 2012  Neop (aka moemneop, email: mneop@web.de)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "dialogs.h"

namespace dia{

gboolean list_places_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer userdata);

void toggle_button_func(GtkToggleButton *togglebutton, gpointer user_data){
    if(gtk_toggle_button_get_active(togglebutton)) *((GtkToggleButton**) user_data) = togglebutton;
}

struct auto_ways_h{
    GtkWidget *checkbutton;
    shared_ptr<place> pl;
    string dir, diro;
    auto_ways_h(GtkWidget *w, shared_ptr<place> p, string d, string d2){
        checkbutton = w;
        pl = p;
        dir = d;
        diro = d2;
    }
};

struct list_places_h{
    void *mapd;
    funcptrivptr goto_place_func;
    int *plid;
    list_places_h(void *md, funcptrivptr goto_func, int *id){
        mapd = md;
        goto_place_func = goto_place;
        plid = id;
    }
};

void world_new(GtkWidget* window_parent, funcptrstr open_world_func){
    string wname = get_str(_("New world"), _("Type in the world name")), worldfile;
    if(wname != ""){
        for(unsigned i = 0; i < wname.length(); i++) if(isalnum(wname[i])) worldfile += wname[i];
        worldfile = path::get_worlds_dir() + worldfile;
        worldmgr::get_instance()->create_world(wname, worldfile);
        open_world_func(worldfile);
    }
}

void world_edit(GtkWidget *parent, shared_ptr<world> w){
    GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Edit world"), GTK_WINDOW(parent), (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);
    gtk_window_set_resizable(GTK_WINDOW(dialog), false);

    GtkWidget *box_h1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), box_h1, true, true, 0);
    GtkWidget *box_v1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v1, true, true, 0);
    GtkWidget *box_v2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v2, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_v1), gtk_label_new(_("World Name")), true, true, 0);
    GtkEntryBuffer *entry_buffer_name = gtk_entry_buffer_new(w->name.c_str(), -1);
    GtkWidget *entry_name = gtk_entry_new_with_buffer(entry_buffer_name);
    gtk_box_pack_start(GTK_BOX(box_v2), entry_name, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_v1), gtk_label_new(_("Color of ways / exits")), true, true, 0);

    GdkRGBA rgba;
    rgba.red = (double)  w->way_col.r / 255;
    rgba.green = (double)  w->way_col.g / 255;
    rgba.blue = (double) w->way_col.b / 255;
    rgba.alpha = 1;
    GtkWidget *col_chooser = gtk_color_button_new_with_rgba(&rgba);
    gtk_box_pack_start(GTK_BOX(box_v2), col_chooser, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_v1), gtk_label_new(_("Color of non-standard ways")), true, true, 0);

    rgba.red = (double)  w->way_ndefault_col.r / 255;
    rgba.green = (double)  w->way_ndefault_col.g / 255;
    rgba.blue = (double) w->way_ndefault_col.b / 255;
    GtkWidget *col_chooser_ndefault = gtk_color_button_new_with_rgba(&rgba);
    gtk_box_pack_start(GTK_BOX(box_v2), col_chooser_ndefault, true, true, 0);

    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), true, true, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), gtk_label_new(_("Danger levels")), true, true, 5);

    GtkWidget *box_h2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), box_h2, true, true, 5);
    GtkWidget *box_v3 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box_h2), box_v3, true, true, 0);
    GtkWidget *box_v4 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box_h2), box_v4, true, true, 0);

    map<int, GtkWidget*> col_chooser_dlc;
    map<int, GtkEntryBuffer*> entrybuffer_dlc;
    map<int, GtkWidget*> entry_dlc;
    vector<int> dlcids = w->get_danger_level_vec();
    for(unsigned i = 0; i < dlcids.size(); i++){
        rgba.red = (double) w->get_danger_level_col(dlcids[i]).r / 255;
        rgba.green = (double) w->get_danger_level_col(dlcids[i]).g / 255;
        rgba.blue = (double) w->get_danger_level_col(dlcids[i]).b / 255;
        rgba.alpha = 1;
        col_chooser_dlc[dlcids[i]] = gtk_color_button_new_with_rgba(&rgba);

        entrybuffer_dlc[dlcids[i]] = gtk_entry_buffer_new(w->get_danger_level_desc(dlcids[i]).c_str(), w->get_danger_level_desc(dlcids[i]).length());
        entry_dlc[dlcids[i]] = gtk_entry_new_with_buffer(entrybuffer_dlc[dlcids[i]]);

        gtk_box_pack_start(GTK_BOX(box_v3), entry_dlc[dlcids[i]], true, true, 0);
        gtk_box_pack_start(GTK_BOX(box_v4), col_chooser_dlc[dlcids[i]], true, true, 0);
    }

    rgba.red = 0.8;
    rgba.green = 0.8;
    rgba.blue = 0.8;
    rgba.alpha = 1;

    col_chooser_dlc[-1] = gtk_color_button_new_with_rgba(&rgba);
    entrybuffer_dlc[-1] = gtk_entry_buffer_new("", -1);

    gtk_box_pack_start(GTK_BOX(box_v3), gtk_entry_new_with_buffer(entrybuffer_dlc[-1]), true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v4), col_chooser_dlc[-1], true, true, 0);

    gtk_widget_show_all(dialog);

    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        string wname = gtk_entry_buffer_get_text(entry_buffer_name);

        worldmgr::get_instance()->remove_available(w->name);
        w->name = wname;
        worldmgr::get_instance()->add_available(w->name, w->file);
        worldmgr::get_instance()->write_available();

        gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(col_chooser), &rgba);
        w->way_col = color(255 * rgba.red, 255 * rgba.green, 255 * rgba.blue);

        gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(col_chooser_ndefault), &rgba);
        w->way_ndefault_col = color(255 * rgba.red, 255 * rgba.green, 255 * rgba.blue);

        for(unsigned i = 0; i < dlcids.size(); i++){
            gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(col_chooser_dlc[dlcids[i]]), &rgba);
            w->set_danger_level(dlcids[i], gtk_entry_buffer_get_text(entrybuffer_dlc[dlcids[i]]), color(255 * rgba.red, 255 * rgba.green, 255 * rgba.blue));
        }
        gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(col_chooser_dlc[-1]), &rgba);
        string dlcdescnew = gtk_entry_buffer_get_text(entrybuffer_dlc[-1]);
        if(dlcdescnew != "") w->set_danger_level(-1, dlcdescnew, color(255 * rgba.red, 255 * rgba.green, 255 * rgba.blue));
    }
    gtk_widget_destroy(dialog);
}

string world_file_chooser(GtkWidget *parent){
    bool file_ok = false;
    string file;
    GtkWidget *dia_filechooser = gtk_file_chooser_dialog_new(_("Open a world file"), GTK_WINDOW(parent), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, nullptr);
    if(gtk_dialog_run(GTK_DIALOG(dia_filechooser)) == GTK_RESPONSE_ACCEPT){
        file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dia_filechooser));
        vector<string> lines = readfile_sv(file, true);
        for(unsigned i = 0; i < lines.size(); i++){
            if(config_comp(lines[i], "wname")){
                file_ok = true;
                break;
            }
        }
    }
    gtk_widget_destroy(dia_filechooser);
    if(file_ok || file == "") return file;
    else {
        GtkWidget *dlg = gtk_message_dialog_new(nullptr, GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_INFO, GTK_BUTTONS_OK, _("The file (%s) doesn't seem to be a world file! If it is please make sure that it is readable."), file.c_str());
        gtk_widget_show_all(dlg);
        gtk_dialog_run(GTK_DIALOG(dlg));
        gtk_widget_destroy(dlg);
    }
    return "";
}

shared_ptr<place> place_dia(GtkWidget *parent, shared_ptr<place> pl, shared_ptr<world> w, int x, int y, int lay){
    GtkWidget *dialog;
    if(pl == nullptr) dialog = gtk_dialog_new_with_buttons(_("Add place"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, GTK_STOCK_ADD, GTK_RESPONSE_OK, nullptr);
    else {
        stringstream ss;
        ss << _("Edit place") << " - " << pl->name << " (ID: " << pl->id << ")";
        dialog = gtk_dialog_new_with_buttons(ss.str().c_str(), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);
    }
    gtk_window_set_resizable(GTK_WINDOW(dialog), false);

    GtkWidget *grid = gtk_grid_new();
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), grid, true, true, 0);

    GtkWidget *label_name = gtk_label_new(_("Place name"));
    gtk_grid_attach(GTK_GRID(grid), label_name, 0, 0, 1, 1);

    GtkEntryBuffer *entry_buffer_name;
    if(pl != nullptr) entry_buffer_name = gtk_entry_buffer_new(pl->name.c_str(), -1);
    else entry_buffer_name = gtk_entry_buffer_new("", 0);
    GtkWidget *entry_name = gtk_entry_new_with_buffer(entry_buffer_name);
    gtk_grid_attach_next_to(GTK_GRID(grid), entry_name, label_name, GTK_POS_RIGHT, 1, 1);

    GtkWidget *label_area = gtk_label_new(_("Area"));
    GtkWidget *combobox_area = gtk_combo_box_text_new();
    gtk_grid_attach_next_to(GTK_GRID(grid), label_area, label_name, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), combobox_area, label_area, GTK_POS_RIGHT, 1, 1);
    vector<pair<int, shared_ptr<area> > > areas = w->get_area_vec_sorted();

    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_area), "-1", _("none"));
    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_area), 0);
    for(unsigned i = 0; i < areas.size(); i++){
        stringstream ss;
        ss << areas[i].first;
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_area), ss.str().c_str(), areas[i].second->name.c_str());
        if(pl != nullptr && areas[i].first == pl->area) gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_area), i + 1);
    }

    GtkWidget *label_dl = gtk_label_new(_("Danger level"));
    GtkWidget *combobox_dl = gtk_combo_box_text_new();
    gtk_grid_attach_next_to(GTK_GRID(grid), label_dl, label_area, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), combobox_dl, label_dl, GTK_POS_RIGHT, 1, 1);
    vector<int> dlc_id = w->get_danger_level_vec();
    for(unsigned i = 0; i < dlc_id.size(); i++){
        stringstream ss;
        ss << dlc_id[i];
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_dl), ss.str().c_str(), w->get_danger_level_desc(dlc_id[i]).c_str());
        if(pl != nullptr && dlc_id[i] == pl->danger_lvl) gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_dl), i);
    }
    if(pl == nullptr) gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_dl), 0);

    GtkWidget *label_reclvl_min = gtk_label_new(_("Reccommended level min"));
    GtkWidget *label_reclvl_max = gtk_label_new(_("Reccommended level max"));
    GtkWidget *spin_button_rl_min = gtk_spin_button_new_with_range(-1, 99999999, 1);
    GtkWidget *spin_button_rl_max = gtk_spin_button_new_with_range(-1, 99999999, 1);
    if(pl != nullptr){
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_button_rl_min), pl->rec_lvl_min);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_button_rl_max), pl->rec_lvl_max);
    }
    gtk_grid_attach_next_to(GTK_GRID(grid), label_reclvl_min, label_dl, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), spin_button_rl_min, label_reclvl_min, GTK_POS_RIGHT, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), label_reclvl_max, label_reclvl_min, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), spin_button_rl_max, label_reclvl_max, GTK_POS_RIGHT, 1, 1);

    GtkWidget *checkb_shop = gtk_check_button_new_with_label(_("Has a shop"));
    GtkWidget *checkb_teacher = gtk_check_button_new_with_label(_("Has a teacher"));
    GtkWidget *checkb_food = gtk_check_button_new_with_label(_("Has (free) food"));
    GtkWidget *checkb_beverages = gtk_check_button_new_with_label(_("Has (free) beverages"));
    if(pl != nullptr){
        if(pl->has_shop) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkb_shop), true);
        if(pl->has_teacher) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkb_teacher), true);
        if(pl->has_free_food) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkb_food), true);
        if(pl->has_free_beverages) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkb_beverages), true);
    }

    gtk_grid_attach_next_to(GTK_GRID(grid), checkb_shop, spin_button_rl_max, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), checkb_teacher, checkb_shop, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), checkb_food, checkb_teacher, GTK_POS_BOTTOM, 1, 1);
    gtk_grid_attach_next_to(GTK_GRID(grid), checkb_beverages, checkb_food, GTK_POS_BOTTOM, 1, 1);

    gtk_widget_show_all(dialog);
    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        bool new_pl = true;
        if(pl != nullptr) new_pl = false;
        string name = gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entry_buffer_name));
        if(name != ""){
            if(new_pl){
                pl = shared_ptr<place>(new place(w->get_new_place_id(), name, w->get_this()));
                pl->posx = x;
                pl->posy = y;
                pl->layer = lay;
            }
            else pl->name = name;
            pl->rec_lvl_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin_button_rl_min));
            pl->rec_lvl_max = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin_button_rl_max));

            stringstream ss(gtk_combo_box_get_active_id(GTK_COMBO_BOX(combobox_dl)));
            ss >> pl->danger_lvl;
            stringstream ssar(gtk_combo_box_get_active_id(GTK_COMBO_BOX(combobox_area)));
            ssar >> pl->area;

            pl->has_shop = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkb_shop));
            pl->has_teacher = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkb_teacher));
            pl->has_free_food = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkb_food));
            pl->has_free_beverages = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkb_beverages));
            if(new_pl) w->add_place(pl);
        }
    }
    gtk_widget_destroy(dialog);
    return pl;
}

shared_ptr<place> place_new(GtkWidget *parent, int x, int y, int lay, shared_ptr<world> w){
    return place_dia(parent, nullptr, w, x, y, lay);
}

void place_edit(GtkWidget *parent, shared_ptr<place> pl, shared_ptr<world> w){
    place_dia(parent, pl, w);
}

void place_edit_marked(GtkWidget *parent, shared_ptr<layer> lay, shared_ptr<world> w){
    GtkWidget *dialog;
    dialog = gtk_dialog_new_with_buttons(_("Edit places"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);
    gtk_window_set_resizable(GTK_WINDOW(dialog), false);

    GtkWidget *box_h1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), box_h1, true, true, 0);
    GtkWidget *box_v1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v1, true, true, 0);
    GtkWidget *box_v2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v2, true, true, 0);

    GtkWidget *label_area = gtk_label_new(_("Area"));
    GtkWidget *combobox_area = gtk_combo_box_text_new();
    gtk_box_pack_start(GTK_BOX(box_v1), label_area, true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v2), combobox_area, true, true, 0);
    vector<pair<int, shared_ptr<area> > > areas = w->get_area_vec_sorted();

    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_area), "-2", _("don't change"));
    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_area), 0);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_area), "-1", _("none"));
    for(unsigned i = 0; i < areas.size(); i++){
        stringstream ss;
        ss << areas[i].first;
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_area), ss.str().c_str(), areas[i].second->name.c_str());
    }

    GtkWidget *label_dl = gtk_label_new(_("Danger level"));
    GtkWidget *combobox_dl = gtk_combo_box_text_new();
    gtk_box_pack_start(GTK_BOX(box_v1), label_dl, true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v2), combobox_dl, true, true, 0);
    vector<int> dlc_id = w->get_danger_level_vec();

    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_dl), "-2", _("don't change"));
    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_dl), 0);
    for(unsigned i = 0; i < dlc_id.size(); i++){
        stringstream ss;
        ss << dlc_id[i];
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combobox_dl), ss.str().c_str(), w->get_danger_level_desc(dlc_id[i]).c_str());
    }

    GtkWidget *label_reclvl_min = gtk_label_new(_("Reccomended level min"));
    GtkWidget *label_reclvl_max = gtk_label_new(_("Reccomended level max"));
    GtkWidget *spin_button_rl_min = gtk_spin_button_new_with_range(-2, 99999999, 1);
    GtkWidget *spin_button_rl_max = gtk_spin_button_new_with_range(-2, 99999999, 1);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_button_rl_min), -2);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin_button_rl_max), -2);

    gtk_widget_set_tooltip_text(spin_button_rl_min, _("-2 = don't change, -1 = not set"));
    gtk_widget_set_tooltip_text(spin_button_rl_max, _("-2 = don't change, -1 = not set"));

    gtk_box_pack_start(GTK_BOX(box_v1), label_reclvl_min, true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v2), spin_button_rl_min, true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v1), label_reclvl_max, true, true, 0);
    gtk_box_pack_start(GTK_BOX(box_v2), spin_button_rl_max, true, true, 0);

    gtk_widget_show_all(dialog);
    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        int rec_lvl_min = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin_button_rl_min));
        int rec_lvl_max = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spin_button_rl_max));
        int area = -1, danger_lvl = -1;

        stringstream ss(gtk_combo_box_get_active_id(GTK_COMBO_BOX(combobox_dl)));
        ss >> danger_lvl;
        stringstream ssar(gtk_combo_box_get_active_id(GTK_COMBO_BOX(combobox_area)));
        ssar >> area;

        for(typename unordered_map<int, unordered_map<int, shared_ptr<place> > >::iterator itx = lay->data.begin(); itx != lay->data.end(); itx++){
            for(typename unordered_map<int, shared_ptr<place> >::iterator ity = itx->second.begin(); ity != itx->second.end(); ity++){
                if(ity->second != nullptr){
                    if(area != -2) ity->second->area = area;
                    if(danger_lvl != -2) ity->second->danger_lvl = danger_lvl;
                    if(rec_lvl_min != -2) ity->second->rec_lvl_min = rec_lvl_min;
                    if(rec_lvl_max != -2) ity->second->rec_lvl_max = rec_lvl_max;
                }
            }
        }
    }
    gtk_widget_destroy(dialog);
}

void place_add_way(GtkWidget *parent, int plid1, string dir1, int plid2, string dir2, shared_ptr<world> w){
    stringstream ss;
    string dir1_str = dir1;
    if(world::way_names.find(dir1) != world::way_names.end()) dir1_str = world::way_names[dir1];
    string dir2_str = dir2;
    if(world::way_names.find(dir2) != world::way_names.end()) dir2_str = world::way_names[dir2];
    ss << _("Do you want to connect") << endl << w->get_place(plid1)->name << ", " << dir1_str << endl << _("with") << endl << w->get_place(plid2)->name << ", " << dir2_str << "?";
    GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, ss.str().c_str(), nullptr);
    gtk_widget_show_all(dlg);
    if(gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_YES){
        w->get_place(plid1)->add_way(dir1, dir2, w->get_place(plid2));
    }
    gtk_widget_destroy(dlg);
}

void auto_ways(shared_ptr<layer> l, shared_ptr<place> pl, GtkWidget* parent){
    GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Automatic way connection"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);

    char str[512];
    sprintf(str, _("Connect %s with:"), pl->name.c_str());
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), gtk_label_new(str), false, true, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL), false, true, 5);

    vector<auto_ways_h> checkboxes;

    for(int x = -1; x <= 1; x++){
        for(int y = -1; y <= 1; y++){
            if(!(x == 0 && y == 0)){
                string dir, diro;
                switch(y){
                    case -1:
                        dir = "s";
                        diro = "n";
                        break;
                    case 1:
                        dir = "n";
                        diro = "s";
                        break;
                }
                switch(x){
                    case -1:
                        dir += "w";
                        diro += "e";
                        break;
                    case 1:
                        dir += "e";
                        diro += "w";
                        break;
                }

                shared_ptr<place> plo = l->get(pl->posx + x, pl->posy + y);
                if(plo != nullptr && pl->ways.count(dir) == 0 && plo->ways.count(diro) == 0){
                    sprintf(str, "[%s] -> [%s] %s", dir.c_str(), diro.c_str(), plo->name.c_str());
                    checkboxes.push_back(auto_ways_h(gtk_check_button_new_with_label(str), plo, dir, diro));
                    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), checkboxes.back().checkbutton, false, true, 0);
                }
            }
        }
    }

    gtk_widget_show_all(dialog);
    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        for(unsigned i = 0; i < checkboxes.size(); i++){
            if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkboxes[i].checkbutton))){
                checkboxes[i].pl->add_way(checkboxes[i].diro, checkboxes[i].dir, pl);
            }
        }
    }
    gtk_widget_destroy(dialog);
}

void list_places(GtkWidget *parent, shared_ptr<world> w, void *mapd, funcptrivptr goto_place_func){
    GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Place list"), GTK_WINDOW(parent), (GtkDialogFlags) (GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
    //gtk_window_set_resizable(GTK_WINDOW(dialog), false);
    gtk_widget_set_size_request(dialog, 450, 500);

    GtkWidget *scrolled_win = gtk_scrolled_window_new(nullptr, nullptr);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolled_win, true, true, 5);

    vector<pair<int, shared_ptr<place> > > places = w->get_place_vec_sorted();
    vector<pair<int, shared_ptr<area> > > areas = w->get_area_vec_sorted();
    map<int, GtkTreeIter> area_iters;
    GtkTreeIter place_iter, cur_iter;
    GtkTreeStore *treestore_places = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);

    gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
    gtk_tree_store_set(treestore_places, &cur_iter, 0, _("without area"), -1);
    area_iters[-1] = cur_iter;
    for(unsigned i = 0; i < areas.size(); i++){
        gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
        gtk_tree_store_set(treestore_places, &cur_iter, 0, areas[i].second->name.c_str(), 2, 0, -1);
        area_iters[areas[i].first] = cur_iter;
    }
    for(unsigned i = 0; i < places.size(); i++){
        gtk_tree_store_append(treestore_places, &place_iter, &area_iters[places[i].second->area]);
        stringstream ss;
        ss << places[i].second->name << " (" << places[i].second->id << ")";
        gtk_tree_store_set(treestore_places, &place_iter, 1, ss.str().c_str(), 2, places[i].first + 1, -1); // place id + 1 weil -1 als default id u.u. als Argumentende gedeutet werden könnte
    }

    GtkWidget *treeview_places = gtk_tree_view_new();
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_places), GTK_TREE_MODEL(treestore_places));
    g_object_unref(treestore_places);
    //gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), treeview_places, true, true, 5);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win), treeview_places);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Area"), gtk_cell_renderer_text_new(), "text", 0, nullptr);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Place"), gtk_cell_renderer_text_new(), "text", 1, nullptr);

    list_places_h h(mapd, goto_place_func, nullptr);

    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview_places));
    gtk_tree_selection_set_select_function(selection, list_places_selection_func, &h, nullptr);

    gtk_widget_show_all(dialog);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

// converts all characters of a string to lower case
string str_tolower(string str){
    string ret;
    for(unsigned i = 0; i < str.length(); ++i){
        char c = str[i];
        ret += c - (c >= 'A' && c <= 'Z' ? 'Z' - 'z' : 0);
    }
    return ret;
}

void search_list_places(GtkWidget *parent, shared_ptr<world> w, void *mapd, funcptrivptr goto_place_func, string search_str){
    GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Search"), GTK_WINDOW(parent), (GtkDialogFlags) (GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
    //gtk_window_set_resizable(GTK_WINDOW(dialog), false);
    gtk_widget_set_size_request(dialog, 450, 500);

    GtkWidget *scrolled_win = gtk_scrolled_window_new(nullptr, nullptr);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolled_win, true, true, 5);

    vector<pair<int, shared_ptr<place> > > places = w->get_place_vec_sorted();
    vector<pair<int, shared_ptr<area> > > areas = w->get_area_vec_sorted();
    map<int, GtkTreeIter> area_iters;
    GtkTreeIter place_iter, cur_iter;
    GtkTreeStore *treestore_places = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);

    gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
    gtk_tree_store_set(treestore_places, &cur_iter, 0, _("without area"), -1);
    area_iters[-1] = cur_iter;
    for(unsigned i = 0; i < areas.size(); i++){
        gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
        gtk_tree_store_set(treestore_places, &cur_iter, 0, areas[i].second->name.c_str(), 2, 0, -1);
        area_iters[areas[i].first] = cur_iter;
    }

    search_str = str_tolower(search_str);
    unsigned number_places = 0;
    for(unsigned i = 0; i < places.size(); i++){
        bool found = false;
        if(str_tolower(places[i].second->name).find(search_str) != string::npos) found = true;
        else {
            for(list<string>::iterator itcomment = places[i].second->comments.begin(); itcomment != places[i].second->comments.end(); itcomment++){
                if(str_tolower(*itcomment).find(search_str) != string::npos){
                    found = true;
                    break;
                }
            }
        }

        if(found){
            ++number_places;
            gtk_tree_store_append(treestore_places, &place_iter, &area_iters[places[i].second->area]);
            stringstream ss;
            ss << places[i].second->name << " (" << places[i].second->id << ")";
            gtk_tree_store_set(treestore_places, &place_iter, 1, ss.str().c_str(), 2, places[i].first + 1, -1); // place id + 1 weil -1 als default id u.u. als Argumentende gedeutet werden könnte
        }
    }

    GtkWidget *treeview_places = gtk_tree_view_new();
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_places), GTK_TREE_MODEL(treestore_places));
    g_object_unref(treestore_places);
    //gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), treeview_places, true, true, 5);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win), treeview_places);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Area"), gtk_cell_renderer_text_new(), "text", 0, nullptr);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Place"), gtk_cell_renderer_text_new(), "text", 1, nullptr);

    list_places_h h(mapd, goto_place_func, nullptr);

    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview_places));
    gtk_tree_selection_set_select_function(selection, list_places_selection_func, &h, nullptr);

    stringstream ss_number_places;
    ss_number_places << number_places << ' ' << _("places found");
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), gtk_label_new(ss_number_places.str().c_str()), false, false, 5);

    gtk_widget_show_all(dialog);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void search(GtkWidget *parent, shared_ptr<world> w, void *mapd, funcptrivptr goto_place_func){
    GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Place / comment search"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_FIND, GTK_RESPONSE_OK, nullptr);
    GtkWidget *box = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

    GtkWidget *box_h = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_pack_start(GTK_BOX(box), box_h, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_h), gtk_label_new(_("search for:")), true, true, 5);
    GtkWidget *entry = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(box_h), entry, true, true, 5);

    gtk_widget_show_all(dialog);

    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        string search_str = gtk_entry_buffer_get_text(gtk_entry_get_buffer(GTK_ENTRY(entry)));

        gtk_widget_destroy(dialog);
        dialog = nullptr;

        search_list_places(parent, w, mapd, goto_place_func, search_str);
    }

    if(dialog) gtk_widget_destroy(dialog);
}

gboolean list_places_selection_func(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer userdata){
    list_places_h *h = (list_places_h*) userdata;

    GtkTreeIter iter;
    if(gtk_tree_model_get_iter(model, &iter, path)){
        int id;
        gtk_tree_model_get(model, &iter, 2, &id, -1);
        if(!path_currently_selected) if(id != 0){
            if(h->goto_place_func != nullptr && h->mapd != nullptr) h->goto_place_func(id - 1, h->mapd);
            if(h->plid != nullptr) *h->plid = id - 1;
        }
    }
    return true;
}

int choose_place(GtkWidget *parent, shared_ptr<world> w){
    GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Place list"), GTK_WINDOW(parent), (GtkDialogFlags) (GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
    gtk_widget_set_size_request(dialog, 450, 500);

    GtkWidget *scrolled_win = gtk_scrolled_window_new(nullptr, nullptr);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolled_win, true, true, 5);

    vector<pair<int, shared_ptr<place> > > places = w->get_place_vec_sorted();
    vector<pair<int, shared_ptr<area> > > areas = w->get_area_vec_sorted();
    map<int, GtkTreeIter> area_iters;
    GtkTreeIter place_iter, cur_iter;
    GtkTreeStore *treestore_places = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);

    gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
    gtk_tree_store_set(treestore_places, &cur_iter, 0, _("without area"), -1);
    area_iters[-1] = cur_iter;
    for(unsigned i = 0; i < areas.size(); i++){
        gtk_tree_store_append(treestore_places, &cur_iter, nullptr);
        gtk_tree_store_set(treestore_places, &cur_iter, 0, areas[i].second->name.c_str(), 2, 0, -1);
        area_iters[areas[i].first] = cur_iter;
    }
    for(unsigned i = 0; i < places.size(); i++){
        gtk_tree_store_append(treestore_places, &place_iter, &area_iters[places[i].second->area]);
        stringstream ss;
        ss << places[i].second->name << " (" << places[i].second->id << ")";
        gtk_tree_store_set(treestore_places, &place_iter, 1, ss.str().c_str(), 2, places[i].first + 1, -1); // place id + 1 weil -1 als default id u.u. als argumentende gedeutet werden könnte
    }

    GtkWidget *treeview_places = gtk_tree_view_new();
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeview_places), GTK_TREE_MODEL(treestore_places));
    g_object_unref(treestore_places);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win), treeview_places);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Area"), gtk_cell_renderer_text_new(), "text", 0, nullptr);
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview_places), -1, _("Place"), gtk_cell_renderer_text_new(), "text", 1, nullptr);

    int plid = -1;
    list_places_h h(nullptr, nullptr, &plid);

    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview_places));
    gtk_tree_selection_set_select_function(selection, list_places_selection_func, &h, nullptr);

    gtk_widget_show_all(dialog);
    if(gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) plid = -1;
    gtk_widget_destroy(dialog);

    return plid;
}

void area_dia(GtkWidget *parent, shared_ptr<world> w, int arid){
    bool new_ar = false;
    shared_ptr<area> ar = nullptr;
    if(arid == -1) new_ar = true;
    else {
        ar = w->get_area(arid);
        if(ar == nullptr) return;
    }

    GtkWidget *dialog;
    if(new_ar) dialog = gtk_dialog_new_with_buttons(_("New area"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT, nullptr);
    else dialog = gtk_dialog_new_with_buttons(((string)_("Edit area") + " - " + ar->name.c_str()).c_str(), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, nullptr);

    GtkWidget *box_h1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), box_h1, true, true, 0);
    GtkWidget *box_v1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v1, true, true, 0);
    GtkWidget *box_v2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(box_h1), box_v2, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_v1), gtk_label_new(_("Area name")), true, true, 0);
    GtkEntryBuffer *entry_buffer_name;
    if(new_ar) entry_buffer_name = gtk_entry_buffer_new("", 0);
    else entry_buffer_name = gtk_entry_buffer_new(ar->name.c_str(), -1);
    GtkWidget *entry_name = gtk_entry_new_with_buffer(entry_buffer_name);
    gtk_box_pack_start(GTK_BOX(box_v2), entry_name, true, true, 0);

    gtk_box_pack_start(GTK_BOX(box_v1), gtk_label_new(_("Color")), true, true, 0);

    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
    gtk_entry_set_activates_default(GTK_ENTRY(entry_name), true);

    GdkRGBA rgba;
    if(new_ar){
        rgba.red = 0.8;
        rgba.green = 0.8;
        rgba.blue = 0.8;
    } else {
        rgba.red = ar->col.get_flt_r();
        rgba.green = ar->col.get_flt_g();
        rgba.blue = ar->col.get_flt_b();
    }
    rgba.alpha = 1;
    GtkWidget *col_chooser = gtk_color_button_new_with_rgba(&rgba);
    gtk_box_pack_start(GTK_BOX(box_v2), col_chooser, true, true, 0);

    gtk_widget_show_all(dialog);
    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT){
        string name = gtk_entry_buffer_get_text(GTK_ENTRY_BUFFER(entry_buffer_name));
        if(name != ""){
            if(new_ar){
                ar = shared_ptr<area>(new area(w->get_new_area_id(), name));
                w->add_area(ar);
            } else ar->name = name;
            gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(col_chooser), &rgba);
            ar->col = color(255 * rgba.red, 255 * rgba.green, 255 * rgba.blue);
        }
    }
    gtk_widget_destroy(dialog);
}

void new_area(GtkWidget *parent, shared_ptr<world> w){
    area_dia(parent, w);
}

void edit_area(GtkWidget *parent, shared_ptr<world> w, int arid){
    area_dia(parent, w, arid);
}

void edit_comments(GtkWidget *parent, shared_ptr<place> pl){
    stringstream title;
    title << _("Edit comments / items") << " (" << pl->name << ")";
    GtkWidget *dialog = gtk_dialog_new_with_buttons(title.str().c_str(), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);
    GtkWidget *box = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

    gtk_box_pack_start(GTK_BOX(box), gtk_label_new(_("One comment per line")), false, false, 5);

    GtkWidget *view;
    GtkTextBuffer *buffer;

    view = gtk_text_view_new();
    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
    stringstream comments;
    for(auto itcomment = pl->comments.begin(); itcomment != pl->comments.end(); itcomment++) comments << *itcomment << endl;
    gtk_text_buffer_set_text(buffer, comments.str().c_str(), -1);
    gtk_box_pack_start(GTK_BOX(box), view, true, true, 0);

    gtk_widget_show_all(dialog);

    if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
        pl->comments.clear();

        GtkTextIter it_start, it_end;
        gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(buffer), &it_start);
        gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(buffer), &it_end);
        stringstream ss_comments(gtk_text_buffer_get_text(buffer, &it_start, &it_end, false));

        while(!ss_comments.eof()){
            char line[1024];
            ss_comments.getline(line, 1024);
            cout << pl->comments.size() << " ";
            if(strcmp(line, "") != 0) pl->comments.push_back(string(line));
            cout << pl->comments.size() << " " << strcmp(line, "") << " " << line << endl;
        }
    }

    gtk_widget_destroy(dialog);
}

string save_as_image(GtkWidget *parent){
    GtkWidget *dlg = gtk_dialog_new_with_buttons(_("Save as image"), GTK_WINDOW(parent), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, nullptr);
    GtkWidget *ca = gtk_dialog_get_content_area(GTK_DIALOG(dlg));

    GtkWidget *radio_button_selected = nullptr;
    GtkWidget *radio_current_screen = gtk_radio_button_new_with_label(nullptr, _("current screen"));
    g_signal_connect(radio_current_screen, "toggled", G_CALLBACK(toggle_button_func), &radio_button_selected);
    gtk_box_pack_start(GTK_BOX(ca), radio_current_screen, false, false, 5);

    GtkWidget *radio_everything = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_current_screen), _("everything on this layer"));
    g_signal_connect(radio_everything, "toggled", G_CALLBACK(toggle_button_func), &radio_button_selected);
    gtk_box_pack_start(GTK_BOX(ca), radio_everything, false, false, 5);

    string ret = "";
    gtk_widget_show_all(dlg);
    if(gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK){
        GtkWidget *file_chooser = gtk_file_chooser_dialog_new(_("Save image as"), GTK_WINDOW(dlg), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, nullptr);
        gtk_widget_show_all(file_chooser);
        if(gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_OK){
            string file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
            if(radio_button_selected == radio_current_screen || radio_button_selected == nullptr) ret = "c" + file;
            else if(radio_button_selected == radio_everything) ret = "e" + file;
        }
        gtk_widget_destroy(file_chooser);
    }
    gtk_widget_destroy(dlg);
    return ret;
}

string get_str(string title, string text, string def){
    string ret = "";
    GtkWidget *dlg = gtk_dialog_new_with_buttons(title.c_str(), nullptr, GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, nullptr);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), gtk_label_new(text.c_str()), true, true, 5);

    GtkEntryBuffer *entry_buffer = gtk_entry_buffer_new(def.c_str(), -1);
    GtkWidget *entry = gtk_entry_new_with_buffer(entry_buffer);
    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dlg))), entry, false, false, 0);

    gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
    gtk_entry_set_activates_default(GTK_ENTRY(entry), true);

    gtk_widget_show_all(dlg);
    if(gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK){
        ret = gtk_entry_buffer_get_text(entry_buffer);
    }
    gtk_widget_destroy(dlg);
    return ret;
}

}
